package com.it.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.http.server.HttpServerResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * TODO
 *
 * @author: Coke
 * @DateTime: 2023/11/12/13:57
 **/

@Controller
@Slf4j
public class CaptchaController {
    
    @GetMapping("/code/image")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response){
        // 创建一个验证码  width – 图片宽 height – 图片高 codeCount – 字符个数 circleCount – 干扰圆圈条数
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
        // 放到session中
        String code = circleCaptcha.getCode();
        request.getSession().setAttribute("CAPTCHA_CODE",code);
        log.info("图片验证码为：{}", code);
    
        try {
            ImageIO.write(circleCaptcha.getImage(),"JPEG",response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
